package cn.stylefeng.guns.sys.modular.system.service;

import cn.hutool.core.math.MathUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.lly835.bestpay.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 需要微信支付订单数据表
 * @Author : xiaofei
 * @Date: 2019/6/19
 */
@Service
public class WxService extends  BestPayServiceImpl{


    Logger log= LoggerFactory.getLogger(WxService.class);

    /**
     * 微信支付
     * @param notifyData
     * @return
     */
    /*public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);//支付方式，微信公众账号支付
        payRequest.setOrderId(orderDTO.getOrderId());//订单号.
        payRequest.setOrderName(payName);//订单名字.
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());//订单金额.
        payRequest.setOpenid(orderDTO.getBuyerOpenid());//微信openid, 仅微信支付时需要
        log.info("【微信支付】request={}", JsonUtil.toJson(payRequest));//将payRequest格式化一下，再显示在日志上，便于观看数据
        PayResponse payResponse = bestPayService.pay(payRequest);
        log.info("【微信支付】response={}", JsonUtil.toJson(payResponse));//将payResponse格式化一下，再显示在日志上，便于观看响应后返回的数据);
        return payResponse;
    }*/


    /*微信支付异步通知*/
    public PayResponse notify(String notifyData) {
        //1. 验证签名
        //2. 支付的状态
        //3. 支付金额
        //4. 支付人(下单人 == 支付人)

        PayResponse payResponse = super.asyncNotify(notifyData);
        log.info("【微信支付】异步通知, payResponse={}", JsonUtil.toJson(payResponse));

        /*//查询订单
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());

        //判断订单是否存在
        if (orderDTO == null) {
            log.error("【微信支付】异步通知, 订单不存在, orderId={}", payResponse.getOrderId());
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }

        //判断金额是否一致(0.10   0.1)
        if (!MathUtil.equals(payResponse.getOrderAmount(), orderDTO.getOrderAmount().doubleValue())) {
            log.error("【微信支付】异步通知, 订单金额不一致, orderId={}, 微信通知金额={}, 系统金额={}",
                    payResponse.getOrderId(),
                    payResponse.getOrderAmount(),
                    orderDTO.getOrderAmount());
            throw new SellException(ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        //修改订单的支付状态
        orderService.paid(orderDTO);*/

        return payResponse;
    }


}
